package com.example.tasks

import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader

const val unsignedApkName = "unsigned"
const val path = "/Users/zhengjianxin/Library/Android/sdk/build-tools/31.0.0"
/**
 * ./zipalign -v -p 4 /Users/zhengjianxin/Downloads/unsigned.apk output.apk
 *
 * -v u输出详细的log
 * -p 对齐的参数。4表示是4字节（32位）边界对齐
 */

/**
 * ./apksigner sign --ks /Users/zhengjianxin/Documents/workspace/trunk/app/keystore/arcsoft.keystore --ks-key-alias arcsoft --key-pass pass:Arcsoft01 --ks-pass pass:Arcsoft01  --out /Users/zhengjianxin/Downloads/signed.apk /Users/zhengjianxin/Downloads/output.apk
 */
fun main() {
    val zipalignPath = "/Users/zhengjianxin/Downloads/zipalign.apk"


    val zipAlignCommand = "$path/zipalign -v -p 4 /Users/zhengjianxin/Downloads/unsigned.apk $zipalignPath"
    val exec = Runtime.getRuntime().exec(zipAlignCommand)
    printLog(exec)

    val signedCommand =
        "$path/apksigner sign --ks /Users/zhengjianxin/Documents/workspace/trunk/app/keystore/arcsoft.keystore --ks-key-alias arcsoft --key-pass pass:Arcsoft01 --ks-pass pass:Arcsoft01  --out /Users/zhengjianxin/Downloads/p365_signed.apk $zipalignPath"
    val process = Runtime.getRuntime().exec(signedCommand)
    printLog(process)

    File(zipalignPath).delete()

    val signedView = "$path/apksigner verify -v /Users/zhengjianxin/Downloads/p365_signed.apk"

    val s = Runtime.getRuntime().exec(signedView)
    printLog(s)
}

fun printLog(process: Process) {
    BufferedReader(InputStreamReader(process.inputStream)).use {
        var line: String?
        while (it.readLine().also { s -> line = s } != null) {
            println(line)
        }

    }
}

